package com.servlet.article;

import com.entity.Code;
import com.service.ArticleService;
import com.utils.JsonUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

import java.io.IOException;

/**
 * 删除文章，根据请求的文章id来删除文章
 * 必选参数：id
 */
@WebServlet("/delArticle")
public class DelArticle extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        ArticleService articleService = new ArticleService();
        JsonUtil jsonUtil = new JsonUtil();

        int art_id = Integer.parseInt(request.getParameter("id"));
        try {
            int i = articleService.delArticleById(art_id);
            if (i==1){
                Code code = new Code();
                code.setCode("200");
                code.setMsg("删除成功");
                String s = jsonUtil.JavaToJson(code);
                response.getWriter().print(s);
            }else {
                Code code = new Code();
                code.setCode("402");
                code.setMsg("该文章不存在");
                String s = jsonUtil.JavaToJson(code);
                response.getWriter().print(s);
            }

        }catch (Exception e){
            System.out.println("删除失败："+e);
            Code code = new Code();
            code.setCode("402");
            code.setMsg("删除失败，请稍后再试");
            String s = jsonUtil.JavaToJson(code);
            response.getWriter().print(s);
        }




    }
}
